package com.mark.article.service.impl;

import com.mark.article.service.AppArticleInfoService;
import com.mark.model.article.dtos.ArticleInfoDto;
import com.mark.model.article.pojos.ApArticleConfig;
import com.mark.model.article.pojos.ApArticleContent;
import com.mark.model.article.pojos.ApAuthor;
import com.mark.model.article.pojos.ApCollection;
import com.mark.model.behavior.pojos.ApBehaviorEntry;
import com.mark.model.behavior.pojos.ApLikesBehavior;
import com.mark.model.behavior.pojos.ApUnlikesBehavior;
import com.mark.model.common.dtos.ResponseResult;
import com.mark.model.common.enums.AppHttpCodeEnum;
import com.mark.model.mappers.app.*;
import com.mark.model.user.pojos.ApUser;
import com.mark.model.user.pojos.ApUserFollow;
import com.mark.utils.common.BurstUtils;
import com.mark.utils.common.ZipUtils;
import com.mark.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description :  java类作用描述
 * @Author :  Markburt
 * @CreateDate :  2020/2/27$ 下午 06:47$
 * @UpdateUser :  Markburt
 * @UpdateDate :  2020/2/27$ 下午 06:47$
 * @UpdateRemark :  Project Build
 * @Version :  1.0
 */
@Service
@SuppressWarnings("all")
public class AppArticleInfoServiceImpl implements AppArticleInfoService {


    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Autowired
    private ApCollectionMapper apCollectionMapper;

    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;


    @Autowired
    private ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;

    @Autowired
    private ApAuthorMapper apAuthorMapper;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    /**
     * 加载文章详情
     *
     * @param articleId
     * @return
     */
    @Override
    public ResponseResult getArticleInfo(Integer articleId) {
        if (articleId == null || articleId < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Map<String, Object> map = new HashMap();

        //根据文章id查询config的信息
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectByArticleId(articleId);
        //判断当前文章是否删除
        if (apArticleConfig == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            //如果没有执行正在删除
        } else if (!apArticleConfig.getIsDelete()) {
            ApArticleContent apArticleContent = apArticleContentMapper.selectByArticleId(articleId);
            String content = ZipUtils.gunzip(apArticleContent.getContent());
            apArticleContent.setContent(content);
            map.put("content", apArticleContent);
        }
        map.put("config", apArticleConfig);

        return ResponseResult.okResult(map);
    }


    /**
     * 加载文章详情的初始化配置信息，比如关注，收藏，点赞，不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        ApUser apUser = AppThreadLocalUtils.getUser();
        //用户与设备不能同时为空
        if (apUser == null && dto.getEquipmentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //通过equipmentId或用户id查看行为实体  --->entryId
        Long userId = null;

        if (apUser != null) {
            userId = apUser.getId();
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectByUserIdOrEquipemntId(
                userId,
                dto.getEquipmentId()
        );

        //定义常量以便判断是否为 true
        boolean isUnis = false,
                isLike = false,
                isCollection = false,
                isFollow = false;

        String burst = BurstUtils.groudOne(apBehaviorEntry.getId());

        //通过entryId和articleId去查看收藏表，看是否有数据，有数据就说明已经收藏，否则就是没有收藏

        ApCollection apCollection = apCollectionMapper.selectForEntryId(
                burst,
                apBehaviorEntry.getId(),
                dto.getArticleId(),
                ApCollection.Type.ARTICLE.getCode()
        );
        if (apCollection != null) {
            isCollection = true;
        }

        //通过entryId和articleId去查看点赞表，看是否有数据，有数据就说明已经点赞，否则就是没有点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorMapper.selectLastLike(
                burst,
                apBehaviorEntry.getId(),
                dto.getArticleId(),
                ApCollection.Type.ARTICLE.getCode());

        if (apLikesBehavior != null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()) {
            isLike = true;
        }
        //通过entryId和articleId去查看不喜欢表，看是否有数据，有数据就说明不喜欢，否则就是喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectLastUnLike(
                apBehaviorEntry.getId(),
                dto.getArticleId()
        );

        if (apUnlikesBehavior != null && apUnlikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()) {
            isUnis = true;
        }


        //通过authorId去app的id  userId(app账号信息id)
        ApAuthor apAuthor = apAuthorMapper.selectById(dto.getAuthorId());
        //查看关注表，根据当前登录人的userId与作者的app账号id去查询，如果有数据，就说明已经关注，没有则说明没有关注
        if (apUser != null && apAuthor != null && apAuthor.getUserId() != null) {
            ApUserFollow apUserFollow = apUserFollowMapper.selectByFollowId(
                    BurstUtils.groudOne(apUser.getId()),
                    apUser.getId(),
                    apAuthor.getUserId().intValue()
            );

            if (apUserFollow != null) {
                isFollow = true;
            }

        }

        Map<String, Object> map = new HashMap<>();
        map.put("isUnis", isUnis);
        map.put("isLike", isLike);
        map.put("isCollection", isCollection);
        map.put("isFollow", isFollow);

        return ResponseResult.okResult(map);
    }


}

